<?php
App::import('Vendor','xtcpdf'); 

$tcpdf = new XTCPDF();
$textfont = 'freesans'; // looks better, finer, and more condensed than 'dejavusans'
$i = 0;

// set document information
$tcpdf->SetCreator(PDF_CREATOR);
$author = $vorname." ".$name;
$tcpdf->SetAuthor($author);
$projectname = $pname." - Mass Swap";
$tcpdf->SetTitle($projectname);
$tcpdf->SetSubject('Detail Report');

// set page properties
$tcpdf->SetAutoPageBreak(false);
$tcpdf->SetPageOrientation('L');
$tcpdf->setPrintHeader(true);
$tcpdf->setPrintFooter(false);
$tcpdf->SetFont('helvetica');

// set header details
$projectname = $pname." - Mass Swap";
$tcpdf->xheadertext = $projectname;
$tcpdf->xheadercolor = array(110, 183, 228);

// add a page
$tcpdf->AddPage();

// page content
$tcpdf->SetTextColor(146, 139, 129);
$tcpdf->SetFontSize(28);
$tcpdf->Text(30,36,'Detail Report for Products');

$maundabt = $vorname." ".$name.", Order Monitoring";
$datumandort = date("d.m.Y").", Otelfingen";
$tcpdf->SetFontSize(16);
$tcpdf->Text(30,60,$maundabt);
$tcpdf->Text(30,67,$datumandort);
$tcpdf->Text(30,90,"Project description: ".$pdescription);
$tcpdf->Image(IMAGES . 'upc-cablecom.jpg', 250, 165, 45, 45, 'JPG');

// page end
$tcpdf->endPage();

// set page properties
$tcpdf->setPrintHeader(false);
$tcpdf->setPrintFooter(true);

// add a page
$tcpdf->AddPage();

// set table properties
$tcpdf->setCellPaddings(1, 1, 1, 1);
$tcpdf->SetFillColor(110, 183, 228);
$tcpdf->SetFontSize(11);

$titel1 = "Product";
$titel2 = "Swaps Total";
$titel3 = "Swaps Open";
$titel4 = "Swaps Closed";

$tcpdf->SetTextColor(255, 255, 255,'',false);
$tcpdf->SetFillColor(168, 212, 239);
$tcpdf->SetFont('helvetica', 'B');
$tcpdf->MultiCell(60,  10, $titel1           , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(40,  10, $titel2           , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(40,  10, $titel3           , 1, 'L', 1, 0, '', '', true);
$tcpdf->MultiCell(40,  10, $titel4           , 1, 'L', 1, 1, '', '', true);
$tcpdf->SetTextColor(0, 0, 0,'',false);
$tcpdf->SetFont('helvetica');
$tcpdf->SetFontSize(11);

$i = 0;
WHILE($row = mysql_fetch_array($result)){
  $productname = $row["reportname"];
  $productid = $row["id"];
  
  // calculations for total macs in that product
  $restul1 = mysql_query("SELECT COUNT(*) count FROM macservicetypes mst, macs m, runshasmacs rhm, runs r WHERE mst.id = '$productid' AND m.macservicetype_id = mst.id AND m.id = rhm.mac_id AND r.id = rhm.run_id AND r.project_id = '$pid'");
  $tm = mysql_fetch_object($restul1)->count;
  
  // calculations for open macs in that product
  $restul2 = mysql_query("SELECT COUNT(*) AS count FROM macservicetypes mst, macs m, runshasmacs rhm, runs r WHERE mst.id = '$productid' AND m.macservicetype_id = mst.id AND m.id = rhm.mac_id AND r.id = rhm.run_id AND r.project_id = '$pid' AND m.status_id IN (26,24)");
  $om = mysql_fetch_object($restul2)->count;
  IF($om != '0' && $tm != '0') { $omp = "(".round(((100/$tm)*$om),0)."%)"; } ELSE { $omp = "(0%)"; }
  
  // calculations for closed macs in that product
  $restul3 = mysql_query("SELECT COUNT(*) AS count FROM macservicetypes mst, macs m, runshasmacs rhm, runs r WHERE mst.id = '$productid' AND m.macservicetype_id = mst.id AND m.id = rhm.mac_id AND r.id = rhm.run_id AND r.project_id = '$pid' AND m.status_id IN (23,25,27,45)");
  $cm = mysql_fetch_object($restul3)->count;
  IF($cm != '0' && $tm != '0') { $cmp = "(".round(((100/$tm)*$cm),0)."%)"; } ELSE { $cmp = "(0%)"; }
 
  // Backgroundcolor
  IF($i++ % 2 == 0) { $tcpdf->SetFillColor(226, 241, 250); } ELSE { $tcpdf->SetFillColor(232, 231, 237); } 
 
  $tcpdf->MultiCell(60,  5, $productname , 1, 'L', 1, 0, '', '', true);
  $tcpdf->MultiCell(40,  5, $tm          , 1, 'L', 1, 0, '', '', true); 
  $tcpdf->MultiCell(40,  5, $om." ".$omp , 1, 'L', 1, 0, '', '', true);
  $tcpdf->MultiCell(40,  5, $cm." ".$cmp , 1, 'L', 1, 1, '', '', true); 
  
}

$tcpdf->SetFont('helvetica');

$tcpdf->SetTextColor(146, 139, 129,'',false);
$tcpdf->SetFontSize(14);
$snapshot = "Data snapshot: ".date("d.m.Y H:i");
$tcpdf->Text(10,183,$snapshot);


// page end
$tcpdf->endPage();


$projectname2 = $pname."-Detail-Products-Report.pdf";
echo $tcpdf->Output($projectname2, 'D');

?>